const tbody=document.querySelector('tbody')
async function score() {
    const res=await axios({
        url:'/score/list'
    })
    const data=res.data.data
    console.log(data);
    let str=''
   for (const key in data) {
    str+=`
    <tr>
        <th scope="row">${key}</th>
        <td>${data[key].name}</td>
        <td class="score" data-id=${key} data-batch=${1}>${data[key].score[0]}</td>
        <td class="score" data-id=${key} data-batch=${2}>${data[key].score[1]}</td>
        <td class="score" data-id=${key} data-batch=${3}>${data[key].score[2]}</td>
        <td class="score" data-id=${key} data-batch=${4}>${data[key].score[3]}</td>
        <td class="score" data-id=${key} data-batch=${5}>${data[key].score[4]}</td>
      </tr>
    `
   }
   tbody.innerHTML=str
}
score() 
tbody.addEventListener('dblclick',function(e){
    if (e.target.classList.contains('score')) {
        const ev=e.target
        const input=document.createElement('input')
        input.type='text'
        ev.append(input)
        input.style.display='block'
        input.value=ev.innerText
        input.focus()
        input.addEventListener('keyup',function(e){
            if (e.key=='Enter') {
                event()
                input.removeEventListener("blur", event);
            }
        })
        input.addEventListener('blur',event)
        async function event(){
                const res=await axios({
                    url:'/score/entry',
                    method:'post',
                    data:{
                        stu_id:e.target.dataset.id,
                        batch:e.target.dataset.batch,
                        score:input.value
                    }
                })
                if (res.data.code==0) {
                    toastr.success(res.data.message)
                    score() 
                }
            
        }
    }
})
